﻿namespace IntellectShop.Web2.Controllers
{
	using System;
	using System.Web;
	using System.Web.Mvc;
	using IntellectShop.Common;
	using IntellectShop.Web2.ViewModels;

	[IntellectShop.Web2.Code.NoCache]
	[IntellectShop.Web2.Code.HandleErrorWithELMAH]
	[ValidateInputAttribute(false)]
	public class PageContentController : Controller
	{
		[ChildActionOnly]
		public ActionResult ViewHeader(string pageContentUniqName)
		{
			var pageContent = Common.PageContent.RetrieveByPageContentUniqName(pageContentUniqName);
			return PartialView(pageContent);
		}

		[ChildActionOnly]
		public ActionResult ViewBody(string pageContentUniqName)
		{
			var viewModel = new PageContentViewModel
				{
					PageContent = Common.PageContent.RetrieveByPageContentUniqName(pageContentUniqName),
					PageContentUniqName = pageContentUniqName
				};

			return PartialView(viewModel);
		}

		//
		// GET: /PageContent/
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Index()
		{
			var pageContent = Common.PageContent.RetrieveAll();
			return View(pageContent);
		}

		//
		// GET: /PageContent/Details/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Details(int pageContentID)
		{
			var pageContent = Common.PageContent.RetrieveByPageContentID(pageContentID);
			return View(pageContent);
		}

		//
		// GET: /PageContent/Create
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Create(string pageContentUniqName)
		{
			var content = new Common.PageContent { PageContentUniqName = pageContentUniqName };
			return View(content);
		}

		//
		// POST: /PageContent/Create
		[Authorize(Roles = Globals.AdministratorRoleName)]
		[HttpPost]
		public ActionResult Create(string pageContentUniqName, Common.PageContent pageContent)
		{
			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: PageContent!");

				pageContent.Create(pageContentUniqName, HttpContext.User.Identity.Name);
				return RedirectToAction("Index");
			}
			catch
			{
				return View(new Common.PageContent());
			}
		}

		//
		// GET: /PageContent/Edit/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Edit(int pageContentID)
		{
			var pageContent = Common.PageContent.RetrieveByPageContentID(pageContentID);
			return View(pageContent);
		}

		//
		// POST: /PageContent/Edit/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		[HttpPost]
		public ActionResult Edit(int pageContentID, FormCollection collection)
		{
			var pageContent = Common.PageContent.RetrieveByPageContentID(pageContentID);
			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: PageContent!");

				UpdateModel(pageContent);
				pageContent.Update(HttpContext.User.Identity.Name);
				return Redirect(Url.RouteUrl(new { action = "Index" }) + "#id" + pageContentID);
			}
			catch
			{
				var pageContentModel = Common.PageContent.RetrieveByPageContentID(pageContentID);
				return View(pageContentModel);
			}
		}

		//
		// GET: /PageContent/Delete/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Delete(int pageContentID)
		{
			var viewModel = new PageContentRemoveViewModel { PageContent = Common.PageContent.RetrieveByPageContentID(pageContentID) };
			return View(viewModel);
		}

		//
		// POST: /PageContent/Delete/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		[HttpPost]
		public ActionResult Delete(int pageContentID, FormCollection collection)
		{
			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: PageContent!");

				Common.PageContent.Delete(pageContentID);
				return RedirectToAction("Index");
			}
			catch (Exception e)
			{
				var viewModel = new PageContentRemoveViewModel
				{
					PageContent = Common.PageContent.RetrieveByPageContentID(pageContentID),
					Message = e.Message
				};
				return View(viewModel);
			}
		}
	}
}
